package com.demo.fulllink.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.demo.fulllink.config.RabbitConfig;
import com.demo.fulllink.module.vo.OrderVO;
import com.demo.fulllink.service.NotifyService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class NotifyConsumer {
    
    @Autowired
    private NotifyService notifyService;
    
    @RabbitListener(
            bindings =
                    {
                            @QueueBinding(value = @Queue(value = RabbitConfig.ORDER_NOTIFY_QUEUE, durable = "true"),
                                    exchange = @Exchange(value = RabbitConfig.ORDER_NOTIFY_QUEUE_EXCHANGE), key = RabbitConfig.ORDER_NOTIFY_KEY)
                    })
    @RabbitHandler
    public void processNotify(Message massage, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        OrderVO orderVO = JSON.parseObject(massage.getBody(), OrderVO.class);
        try {
            notifyService.notifyHandel(orderVO);
            channel.basicAck(tag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
